﻿#include "dlgmain.h"
#include "ui_dlgmain.h"

#include <QQmlEngine>
#include <QQmlContext>
#include <QQuickItem>
#include <QDebug>
#include <QJsonDocument>
#include <QJsonObject>

QRect g_FirstScreen;

DlgMain::DlgMain(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DlgMain)
{
    ui->setupUi(this);
    m_pDesktop = QApplication::desktop();
    g_FirstScreen = m_pDesktop->screenGeometry(0);
//    this->setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);
//    this->setFixedSize(g_FirstScreen.width(), g_FirstScreen.height());
//    ui->lineEdit->move(g_FirstScreen.width()/2, g_FirstScreen.height() - ui->lineEdit->height() - 100);

    ui->quickWidget->setResizeMode(QQuickWidget::SizeRootObjectToView);
    ui->quickWidget->setSource(QUrl::fromLocalFile("vkb.qml"));
//    ui->quickWidget->setSource(QUrl("vkb.qml"));
    QQuickItem *item = ui->quickWidget->rootObject();
    QString name = item->objectName();
    qDebug() << "objectname=" << name.toUtf8().data();
//    connect(ui->quickWidget->rootObject(), SIGNAL(sigSendToQt(QString)), this, SLOT(slotRecvFromQml(QString)));
//    connect(this,SIGNAL(sigSendToQml(QString)),ui->quickWidget->rootObject(),SIGNAL(sigRecvFromQt(QString)));


    //
    ui->quick1->setResizeMode(QQuickWidget::SizeRootObjectToView);
    ui->quick1->setSource(QUrl::fromLocalFile("dashboard/content/synjones.qml"));
    connect(this,SIGNAL(sigSend2QML(QString)),ui->quickWidget->rootObject(),SIGNAL(sigRecvFromQt(QString)));
}

DlgMain::~DlgMain()
{
    delete ui;
}

void DlgMain::slotRecvFromQml(QString strData)
{
    qDebug() << "Recv From Qml data=" << strData.toUtf8().data();
}

void DlgMain::on_pushButton_initqml_clicked()
{
    QJsonObject obj;
    obj.insert("msgType",1);
    obj.insert("data","hello world!");
    QJsonDocument doc;
    doc.setObject(obj);
    QByteArray result = doc.toJson(QJsonDocument::Compact);
    emit sigSendToQml(QString::fromUtf8(result));
}

void DlgMain::on_pushButton_clicked()
{
    QJsonObject obj;
    obj.insert("msgType",1);
    obj.insert("data","hello 11111world!");
    QJsonDocument doc;
    doc.setObject(obj);
    QByteArray result = doc.toJson(QJsonDocument::Compact);
    emit sigSend2QML(QString::fromUtf8(result));
}
